package com.bolt.admin.module.sys.repository;


import com.bolt.admin.module.sys.entity.UserEntity;
import com.bolt.admin.module.sys.entity.UserR2Role;
import com.bolt.support.spring.jpa.repository.BasicJpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface UserR2RoleRepository extends BasicJpaRepository <UserR2Role, String> {

    @Query("select r2 from RoleEntity r, UserR2Role r2 where r=r2.role and r2.user.id=:userId and r.enabled=true ")
    List <UserR2Role> findEnabledRolesForUser(@Param("userId") Long userId);


    @Query("select r2 from UserEntity u, UserR2Role r2, RoleEntity r where r=r2.role and u=r2.user and r2.user=:user order by r.code")
    List <UserR2Role> findByUser(@Param("user") UserEntity user);


    @Query("select distinct u from UserEntity u, UserR2Role r2, RoleEntity r where r=r2.role and u=r2.user and r.id = :roleId order by r.code")
    List <UserEntity> findUserByRoleId(@Param("roleId") String roleId);

}
